/*
 * main.cpp
 *
 *  Created on: Dec 17, 2013
 *      Author: vipatil
 */

#include "Logger.h"
#include "Config.h"
#include "ServerThread.h"
#include "Constants.h"
#include "CmdCenter.h"

#include <iostream>
#include <vector>
#include <memory>
#include <log4cpp/Category.hh>

void init() {

  Logger log;

  log4cpp::Category& logger =
      log4cpp::Category::getInstance(std::string(SERVER_LOGGER));

  try {

    logger.info("**********  Server Started ************");

    std::unique_ptr<Configuration::Config> config(Configuration::Config::getConfig("server.cfg"));

    ServerThread::ServerThread ser;
    CmdCenter::CmdCenter cmdCenter(ser);

    //Start command center thread
    std::unique_ptr<std::thread> cmdTh (new std::thread(std::ref(cmdCenter)));


    //start server listen thread
    std::unique_ptr<std::thread> sThread(new std::thread(std::ref(ser)));//

    //cmdCenter.setServerPtr(&ser);

    //wait for command center thread to finish
    cmdTh->join();

    //command center is done notify
    ser.setExitFlag();

    //TODO:dummy connection to return from accept

    //wait now for server thread to finish
    sThread->join();

    logger.info("Exiting server....");

  }
  catch (std::exception& ex) {
    logger.error(std::string("init failed : ")+ex.what());

  }
}

int main()
{
  init();
  return 0;
}
